<?php

function addFeaturesDetails($vip_t){
    global $lang;
    $addHidden = true;
    if($vip_t == null)
        $addHidden = false;
        
    echo '<table class="tab_details fill">
          <tr>
            <th class="tittle" colspan="2">'.$lang['FEATURES'].'</th>
          </tr><tr>';
    addCheckbox('LONG_JUMP', 'long_jump', $vip_t, false, null);
    echo '</tr><tr>';
    addCheckbox('DOUBLE_JUMP', 'double_jump', $vip_t, false, null);      
    echo '</tr><tr>';
    addCheckbox('SEE_DMG', 'see_damage', $vip_t, false, null);  
    echo '</tr><tr></tr><tr>
            <th class="tittle" colspan="2">'.$lang['ADMIN_COMMAND_WHEN_NO_ADMIN'].'</th>
          </tr><tr>';
    addCheckbox('CMD_BAN', 'ban', $vip_t, false, null);
    echo '</tr><tr>';
    addCheckbox('CMD_KICK', 'kick', $vip_t, false, null);
    echo '</tr><tr>'; 
    addCheckbox('CMD_SS', 'ss', $vip_t, false, null);
    echo '</tr>
        </table>';
}


function addVipTmplAHC($ahc){
    global $lang;

    echo '<table class="tab_details fill">
          <tr>
            <th class="tittle" colspan="2">'.$lang['ARMOUT_HEALTH_KASH'].'</th>
          </tr><tr>';
    addText('ARMOUR', 'armour', $ahc, '0', null, true);       
    echo '</tr><tr>';
    addText('MAX_HP', 'hp_max', $ahc, '0', null, true);
    echo '</tr><tr>';
    addText('HP_KILL', 'hp_kill', $ahc, '0', null, true);
    echo '</tr><tr>';
    addText('HP_HS', 'hp_hs', $ahc, '0', null, true);
    echo '</tr><tr>';
    addText('CASH_ROUND', 'cash_round', $ahc, '0', null, true);
    echo '</tr><tr>';
    addText('CASH_KILL', 'cash_kill', $ahc, '0', null, true);
    echo '</tr><tr>';
    addText('CASH_HS', 'cash_hs', $ahc, '0', null, true);
    echo '</tr>
        </table>';
}


function addVipTmplWeapons($wpn){
    global $lang;

    echo '<table class="tab_details fill">
          <tr>
            <th class="tittle" colspan="2">'.$lang['WEAPON_FEATURES'].'</th>
          </tr>';
    addCheckbox('AMMO', 'ammo', $wpn, false, null); 
    echo '</tr><tr>';     
    addText('DEAGLE_ROUND', 'deagle_round', $wpn, '0', null, true);
    echo '</tr>';            
    $nades = array("he", "fb", "sg");
    foreach($nades as $nade){
        echo '<tr>';
        addText(strtoupper($nade), $nade, $wpn, '0', null, true);
        echo '</tr><tr>'; 
        addText(strtoupper($nade).'_ROUND', $nade.'_round', $wpn, '0', null, true);
        echo '</tr>';
      }      
    echo '</table>';
}

function addVipTmplPrimWeapons($primWpn){
    global $lang;

    echo '<table class="tab_details fill">
          <tr>
            <th class="tittle" colspan="4">'.$lang['PRIMATY_WEAPON_FEATURES'].'</th>
          </tr>';
       
    $weapons = array("mac10_round", "aug_round", "ump45_round", 
                    "sg550_round", "galil_round", "famas_round", "awp_round", 
                    "mp5navy_round", "m249_round", "m3_round",
                    "m4a1_round", "tmp_round", "g3sg1_round", "sg552_round", 
                    "ak47_round", "p90_round");
    $openTr = true; 
    $closeTr = false;
    foreach($weapons as $wpn){
        if($openTr){
            echo '<tr>';
        }
        addText(strtoupper($wpn), $wpn, $primWpn, '0', null, true);
        if($closeTr) {
            echo '</tr>';
        }
        $openTr = !$openTr;
        $closeTr = !$closeTr;
    }    
    echo'</table>';
}

function updateVipT(){
    mysql_query("SET AUTOCOMMIT=0");
    mysql_query("START TRANSACTION");
    
    $commit = true;
    global $prefix;
    $fields = array(
        'long_jump' => 0,
        'double_jump' => 0,
        'see_damage' => 0,
        'ban' => 0,
        'kick' => 0,
        'ss' => 0
    );
    $id = $_POST['id'];
  
    $query = createUpdateStatement($fields, $prefix.'_VIP_TMPL', true, 'id', $id);
    $result = mysql_query($query);
    if(!$result){
        $error = mysql_error();
        echo $error;
		echo '<br/>';
		$commit = false;
    } 
    if($commit){
        $fields = array(
            'armour' => 0,
            'hp_max' => 0,
            'hp_kill' => 0,
            'hp_hs' => 0,
            'cash_round' => 0,
            'cash_kill' => 0,
            'cash_hs' => 0
        );
        
        $query = createUpdateStatement($fields, $prefix.'_VIP_TMPL_AHC', true, 'vip',$id);
        $result = mysql_query($query);
        if(!$result){
            $error = mysql_error();
            echo $error;
    		echo '<br/>';
    		$commit = false;
        } 
    }
    if($commit){
        $fields = array(
            'ammo' => 0,
            'deagle_round' => 0,
            'he' => 0,
            'he_round' => 0,
            'fb' => 0,
            'fb_round' => 0,
            'sg' => 0,
            'sg_round' => 0
        );
        
        $query = createUpdateStatement($fields, $prefix.'_VIP_TMPL_WPN', true, 'vip',$id);
        $result = mysql_query($query);
        if(!$result){
            $error = mysql_error();
            echo $error;
    		echo '<br/>';
    		$commit = false;
        } 
    }
    
    if($commit){
        $fields = array(
            'mac10_round' => 0,
            'aug_round' => 0,
            'ump45_round' => 0,
            'sg550_round' => 0,
            'galil_round' => 0,
            'famas_round' => 0,
            'awp_round' => 0,
            'mp5navy_round' => 0,
            'm249_round' => 0,
            'm3_round' => 0,
            'm4a1_round' => 0,
            'tmp_round' => 0,
            'g3sg1_round' => 0,
            'sg552_round' => 0,
            'ak47_round' => 0,
            'p90_round' => 0
        );
        
        $query = createUpdateStatement($fields, $prefix.'_VIP_TMPL_WPN_PRIM', true, 'vip',$id);
        $result = mysql_query($query);
        if(!$result){
            $error = mysql_error();
            echo $error;
    		echo '<br/>';
    		$commit = false;
        }
    }
    
    if($commit){
        mysql_query("COMMIT");
    } else {        
        mysql_query("ROLLBACK");
    }
}
function createNewVipT(){
    mysql_query("SET AUTOCOMMIT=0");
    mysql_query("START TRANSACTION");
    
    $commit = true;
    global $prefix;
    global $lang;
    
    $name='';
    if(isset($_POST['name']))
        $name = trim($_POST['name']);
    if($name == ''){
        echo $lang['NAME_REQ'];
        echo '<br/>';
        return;
    }
    $query = 'SELECT 1 FROM '.$prefix.'_VIP_TMPL WHERE name = \''.mysql_escape_string($name).'\'';
    $result = mysql_query($query);
    if($result){
        if(mysql_num_rows($result)>0){
            echo $lang['NAME_TAKEN'];
            echo '<br/>';
            $commit = false;
        }
    }
    if($commit){
        $fields = array(
            'name' => '',
            'long_jump' => 0,
            'double_jump' => 0,
            'see_damage' => 0,
            'ban' => 0,
            'kick' => 0,
            'ss' => 0
        );
        $query = createInsertStatement($fields, $prefix.'_VIP_TMPL', true);
        $result = mysql_query($query);
        if(!$result){
            $error = mysql_error();
            echo $error;
    		echo '<br/>';
    		$commit = false;
        } 
    }
    if($commit){
        $vip_id=mysql_insert_id();
        $fields = array(
            'vip' => $vip_id,
            'armour' => 0,
            'hp_max' => 0,
            'hp_kill' => 0,
            'hp_hs' => 0,
            'cash_round' => 0,
            'cash_kill' => 0,
            'cash_hs' => 0
        );
        
        $query = createInsertStatement($fields, $prefix.'_VIP_TMPL_AHC', true);
        $result = mysql_query($query);
        if(!$result){
            $error = mysql_error();
            echo $error;
    		echo '<br/>';
    		$commit = false;
        } 
    }
    if($commit){
        $fields = array(
            'vip' => $vip_id,
            'ammo' => 0,
            'deagle_round' => 0,
            'he' => 0,
            'he_round' => 0,
            'fb' => 0,
            'fb_round' => 0,
            'sg' => 0,
            'sg_round' => 0
        );
        
        $query = createInsertStatement($fields, $prefix.'_VIP_TMPL_WPN', true);
        $result = mysql_query($query);
        if(!$result){
            $error = mysql_error();
            echo $error;
    		echo '<br/>';
    		$commit = false;
        } 
    }
    
    if($commit){
        $fields = array(
            'vip' => $vip_id,
            'mac10_round' => 0,
            'aug_round' => 0,
            'ump45_round' => 0,
            'sg550_round' => 0,
            'galil_round' => 0,
            'famas_round' => 0,
            'awp_round' => 0,
            'mp5navy_round' => 0,
            'm249_round' => 0,
            'm3_round' => 0,
            'm4a1_round' => 0,
            'tmp_round' => 0,
            'g3sg1_round' => 0,
            'sg552_round' => 0,
            'ak47_round' => 0,
            'p90_round' => 0
        );
        
        $query = createInsertStatement($fields, $prefix.'_VIP_TMPL_WPN_PRIM', true);
        $result = mysql_query($query);
        if(!$result){
            $error = mysql_error();
            echo $error;
    		echo '<br/>';
    		$commit = false;
        }
    }
    
    if($commit){
        mysql_query("COMMIT");
    } else {        
        mysql_query("ROLLBACK");
    }
}
function deleteVipT(){
    global $prefix;
    $query = 'DELETE FROM '.$prefix.'_VIP_TMPL WHERE id = '.$_POST['id'];
    $result = mysql_query($query);
	if(!$result){
        echo mysql_error();
        echo '<br/>';
        mysql_close();		
    }
}

function showVipTDetails($id, $isEdit){
    global $lang;
    global $prefix;
    $isNew = false;
    if($id != null){
        $query = 'SELECT * FROM '.$prefix.'_VIP_TMPL WHERE id = '.$id;
    	$result = mysql_query($query);
    	if(!$result){
            echo mysql_error();
            echo '<br/>';
            mysql_close();
    		die($error);
        } else {
            $vip_t = mysql_fetch_array($result);
        }
        $query = 'SELECT * FROM '.$prefix.'_VIP_TMPL_AHC WHERE vip = '.$id;
        $result = mysql_query($query);
    	if(!$result){
            echo mysql_error();
            echo '<br/>';
            mysql_close();
    		die($error);
        }else {
            $ahc = mysql_fetch_array($result);
        }
        
        $query = 'SELECT * FROM '.$prefix.'_VIP_TMPL_WPN WHERE vip = '.$id;
        $result = mysql_query($query);
    	if(!$result){
            echo mysql_error();
            echo '<br/>';
            mysql_close();
    		die($error);
        }else {
            $wpn = mysql_fetch_array($result);
        }
        $query = 'SELECT * FROM '.$prefix.'_VIP_TMPL_WPN_PRIM WHERE vip = '.$id;
        $result = mysql_query($query);
    	if(!$result){
            echo mysql_error();
            echo '<br/>';
            mysql_close();
    		die($error);
        }else {
            $primWpn = mysql_fetch_array($result);
        }
    }
    else {
        $vip_t = null;
        $ahc = null;
        $wpn = null;
        $primWpn = null;
        $isNew = true;
    }
	echo '         <form name="form_vip_t" method="POST" action="main.php?action=vip_t">
                      <input name="id" type="hidden" value="'.$vip_t['id'].'"/>
                      <table class="fill">
                        <tr>
                          <td>
                            <table class="tab_details">	
                              <tr>';
                              if($isNew){
                                    addText('NAME', 'name', null, '', null, false);
                              } else {
                                echo '  <th class="half right">'.$lang['NAME'].'</th>
                                        <td class="left readonly_value">
                                          '.$vip_t['name'].'
                                        </td>';
                              }
                              echo'  
                              </tr>
                              <tr>
                                <td class="half">';
                              addFeaturesDetails($vip_t);
                          echo '</td><td class="fill">';
                              addVipTmplAHC($ahc);
                          echo '</td></tr>
                          <tr><td class="half">';
                              addVipTmplWeapons($wpn);
                          echo '</td><td class="fill">';
                              addVipTmplPrimWeapons($primWpn);
                          echo'
                              </td>
      				                </tr>
                            </table>
                          </td>
                        </tr>';
    if($isNew || hasAccess('edit_vips_t') || hasAccess('del_vips_t')) { 
        echo '<tr><td colspan="2" class="fill right">';
    }          
                      
    if($isNew) {
        echo '<input class="btn" type="submit" name="ins" value="'.$lang['B_SAVE'].'"/>';
    } else if(hasAccess('edit_vips_t')) {
        echo '<input class="btn" type="button" name="edit" value="'.$lang['B_EDIT'].'" onclick="toogleInputs();"/>'; 
        echo '<input class="btn save" type="submit" disabled="true" name="upd" id="upd" value="'.$lang['B_SAVE'].'"/>';
    } 
    if(!$isNew && hasAccess('del_vips_t')){
        echo '<input class="btn" type="submit" name="del" value="'.$lang['B_DELETE'].'" onclick="return confirmDelete()"/>';
    }
    if($isNew || hasAccess('edit_vips_t') || hasAccess('del_vips_t')) { 
        echo '</td></tr>';
    } 
    echo '            </table></form>';
    
    if($isEdit || $isNew){
        echo '<script type="text/javascript">
                enableInputs(true);
        </script>';
    } 
}
?>
